package com.example.XueCuApp.View.Xuechu;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.example.XueCuApp.Adapter.TicketPrintAgainAdaprer;
import com.example.XueCuApp.Base.BaseActivity;
import com.example.XueCuApp.Bean.JJHXiaoPiaoDaYingBean;
import com.example.XueCuApp.Bean.RepeatPrintCheckRecordRequsetBean;
import com.example.XueCuApp.Bean.RepeatPrintCheckRecordResultBean;
import com.example.XueCuApp.PrintHelp.PrintBillService;
import com.example.XueCuApp.R;
import com.example.XueCuApp.RetrufitHelp.BaseSubscriber;
import com.example.XueCuApp.RetrufitHelp.ExceptionHandle;
import com.example.XueCuApp.RetrufitHelp.RetrofitClient;
import com.example.XueCuApp.Utils.ListDataSave;
import com.example.XueCuApp.Utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

public class TicketPrintAgainActivity extends BaseActivity {

    private RecyclerView rvTicketAgain;

    private TicketPrintAgainAdaprer printAgainAdaprer;
    private TextView tvBackCheck;

    private ListDataSave listDataSave;
    private List<JJHXiaoPiaoDaYingBean> againList;
    private RepeatPrintCheckRecordRequsetBean recordRequsetBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public int initLayoutId() {
        return R.layout.activity_ticket_print_again;
    }

    @Override
    protected void initView() {

        if (recordRequsetBean == null) {
            recordRequsetBean = new RepeatPrintCheckRecordRequsetBean();
        }

        // 获取小票数据
        if (againList == null) {
            againList = new ArrayList<>();
        }

        listDataSave = new ListDataSave(TicketPrintAgainActivity.this,"xiaopiao");
        againList = listDataSave.getDataList("xiaopiaoshuju",JJHXiaoPiaoDaYingBean.class);


        tvBackCheck = findViewById(R.id.tv_new_xin_ticket_again);
        rvTicketAgain = findViewById(R.id.rv_printagain);
        rvTicketAgain.setLayoutManager(new LinearLayoutManager(TicketPrintAgainActivity.this, RecyclerView.VERTICAL, false));
        printAgainAdaprer = new TicketPrintAgainAdaprer(TicketPrintAgainActivity.this, againList);
        rvTicketAgain.setAdapter(printAgainAdaprer);

        printAgainAdaprer.SetPrintAgainListener(new TicketPrintAgainAdaprer.PrintAgainListener() {
            @Override
            public void printAgain(int pos) {
                getPrintDate(againList.get(pos).getOrderCheckBatchNo());
            }
        });

    }

    private void getPrintDate(String orderCheck) {
        recordRequsetBean.setOrderCheckBatchNo(orderCheck);
        RetrofitClient.getInstance(TicketPrintAgainActivity.this, BaseUrl, headers).repeatPrintCheckRecord(recordRequsetBean, new BaseSubscriber<RepeatPrintCheckRecordResultBean>(TicketPrintAgainActivity.this) {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable e) {
                ToastUtil.showToast(e.message);
            }

            @Override
            public void onNext(RepeatPrintCheckRecordResultBean repeatPrintCheckRecordResultBean) {
                if (repeatPrintCheckRecordResultBean.getCode().equals("200")) {
                    if (repeatPrintCheckRecordResultBean.getData() != null) {
                        printPaper(repeatPrintCheckRecordResultBean.getData());
                    }else {
                        ToastUtil.showToast("打印数据对象为空");
                    }
                } else {
                    ToastUtil.showToast(repeatPrintCheckRecordResultBean.getMessage());
                }

            }
        });
    }

    public void printPaper(RepeatPrintCheckRecordResultBean.DataBean print) {

        Intent intentService = new Intent(this, PrintBillService.class);
        intentService.putExtra("SPRT", print);
        startService(intentService);
    }

    @Override
    protected void initData() {

        tvBackCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }
}
